package com.zretc.kscxmz.services.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zretc.kscxmz.entity.Discuss;
import com.zretc.kscxmz.mappers.DiscussMapper;
import com.zretc.kscxmz.services.DiscussService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author zengyanjun
 * @date 2020-07-25
 */
@Service
public class DiscussServiceImpl implements DiscussService {
    Logger logger = LoggerFactory.getLogger(DiscussServiceImpl.class);
    @Autowired
    private DiscussMapper mapper;
    @Override
    public int insert(Discuss discuss) {
        return mapper.insert(discuss);
    }

    @Override
    public int modify(Discuss discuss) {
        return mapper.update(discuss);
    }

    @Override
    public PageInfo<Discuss> get(Map<String, String> map) {
        //1、开启分页
        PageHelper.startPage(Integer.valueOf(map.get("pageNum")),Integer.valueOf(map.get("pageSize")));
        Map<String,String> condition = new HashMap<>();
        if (!"".equals(map.get("condition"))){
            logger.info("condition值不为空");
            condition.put("condition","%"+map.get("condition")+"%");
        }else{
            condition.put("condition",null);
        }
        if (!"".equals(map.get("userId"))){
            logger.info("userId值不为空");
            condition.put("userId",map.get("userId"));
        }else{
            condition.put("userId",null);
        }

        List<Discuss> discusses = mapper.find(condition);
        //2、进行封装
        PageInfo<Discuss> pageInfo = new PageInfo<>(discusses);
        return pageInfo;
    }

    @Override
    public Discuss getDiscussById(Integer discussId) {
        return mapper.findById(discussId);
    }

    @Override
    public Discuss findDiscussDetailsById(Integer discussId,String visitNum) {
        if(!"".equals(visitNum)){
            //增加访问量
            int n = mapper.addVisitNum(discussId,Integer.valueOf(visitNum)+1);
        }

        return mapper.findDiscussDetailsById(discussId);
    }

    @Override
    public int addReplyOrComment(String commentId, String replytype, String comment, String userId) {
        if ("一级评论".equals(replytype)){
            return mapper.addReply(Integer.valueOf(commentId),Integer.valueOf(userId),comment);
        }else{
            return mapper.addComment(Integer.valueOf(commentId),Integer.valueOf(userId),comment);
        }
    }

    @Override
    public int removeReplyOrComment(String commentId, String replytype) {
        if ("回复".equals(replytype)){
            //先删除该回复下的所有评论
            mapper.deleteCommentByReplyId(Integer.valueOf(commentId));
            return mapper.deleteReplyById(Integer.valueOf(commentId));
        }else{
            return mapper.deleteCommentById(Integer.valueOf(commentId));
        }
    }

}
